package com.wanwanbear.www.service.impl;

import com.wanwanbear.www.mapper.MoviesMapper;
import com.wanwanbear.www.pojo.Movies;
import com.wanwanbear.www.pojo.MoviesExample;
import com.wanwanbear.www.service.MovieService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MoviesMapper moviesMapper;

    @Scheduled(fixedDelay = 24*60*60*1000)
    public void getMovie() {
        Movies movies = new Movies();
        String url = "http://www.dytt8.net/";
        try {
            Document doc = Jsoup.connect(url)
                    //.data("query", "Java")
                    //.userAgent("头部")
                    //.cookie("auth", "token")
                    //.timeout(3000)
                    //.post()
                    .get();
            //System.out.println(doc);
            //得到html的所有东西
            Element content = doc.getElementById("header");
            //Elements movies = doc.getElementsByClass("inddline");
            //分离出html下<a>...</a>之间的所有东西
            //Elements links = content.getElementsByTag("a");
            Elements links = doc.select("td.inddline>a[href]");
            // 扩展名为.png的图片
            Elements pngs = doc.select("img[src$=.png]");
            // class等于masthead的div标签
            Element masthead = doc.select("div.masthead").first();

            for (Element link : links) {
                //得到<a>...</a>里面的网址
                String linkHref = link.attr("href");
                //得到<a>...</a>里面的汉字
                String linkText = link.text();
                //System.out.println(linkText);
                movies.setMovieName(linkText);
                System.out.println(linkHref);
                System.out.println(linkText);
                Movies changeMovies = getImgAndUrl(url + "" + linkHref, movies);
                //Document document = Jsoup.connect(url+""+linkHref).get();
                // System.out.println(document);
                if (changeMovies!=null&&changeMovies.getMovieDownloadUrl() != null && (!changeMovies.getMovieDownloadUrl().equals(""))) {
                    MoviesExample moviesExample = new MoviesExample();
                    MoviesExample.Criteria criteria = moviesExample.createCriteria();
                    criteria.andMovieBannerImgUrlEqualTo(changeMovies.getMovieBannerImgUrl());
                    if(moviesMapper.selectByExample(moviesExample).isEmpty()&&!changeMovies.getMovieDownloadUrl().contains(".exe")&&!changeMovies.getMovieDownloadUrl().contains(".rar")) {
                        moviesMapper.insert(changeMovies);
                    }
                }
            }
        } catch (
                IOException e)

        {
            e.printStackTrace();
        }

    }

    public List<Movies> selectMovieSupportApp() {
        MoviesExample moviesExample = new MoviesExample();
        //MoviesExample.Criteria criteria = moviesExample.createCriteria();
        return moviesMapper.selectByExample(moviesExample);
    }

    public void updateFileState(Movies movies) {
        moviesMapper.updateByPrimaryKeySelective(movies);
    }

    public List<Movies> selectByName() {
        MoviesExample moviesExample = new MoviesExample();
        MoviesExample.Criteria criteria = moviesExample.createCriteria();
        criteria.andMovieNameLike("%高分%");
        return moviesMapper.selectByExample(moviesExample);
    }


    public Movies getImgAndUrl(String url, Movies movies) {
        try {
            Document doc = Jsoup.connect(url)
                    //.data("query", "Java")
                    //.userAgent("头部")
                    //.cookie("auth", "token")
                    //.timeout(3000)
                    //.post()
                    .get();
            Elements pngs = doc.select("div#Zoom img[src$=.jpg]");
            if (!pngs.isEmpty()) {
                String pngUrl = pngs.first().attr("src");
                String contentUrl = pngs.last().attr("src");
                movies.setMovieBannerImgUrl(pngUrl);
                System.out.println(pngUrl+"  "+contentUrl);
                Elements downloadUrl = doc.select("table a[href]");
                if (!downloadUrl.isEmpty()) {
                    String download = downloadUrl.first().attr("href");
                    movies.setMovieDownloadUrl(download);
                    System.out.println(download);
                    movies.setIsDownload(false);
                }
                return movies;
            }else{
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
